#!/bin/bash

# host_addr=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
host_addr=`ifconfig -a|grep inet|grep -v inet6|grep -v 127.0.0.1|perl -pe 's/.*inet\s?(.*?)\s+.*/\1/g'`;
base_dir=`cd $(dirname $0); pwd -P`;

function printSuccess(){
  echo -e "\033[92m\033[1m"$1"\033[0m";
}

function printFail(){
  echo -e "\033[91m\033[1m"$1"\033[0m";
}

function printInfo(){
  echo -e "\033[96m\033[1m"$1"\033[0m";
}

function printWarn(){
  echo -e "\033[101m\033[33m\033[1m"$1"\033[0m";
}

loadmsg_time=0;
function loadMsg(){
  let loadmsg_time++
  echo -ne "\r\033[33m\033[1m"$1"("$loadmsg_time"s)\033[0m\r";
}

function ostype(){
  os=`lsb_release -a|grep "Distributor ID"|awk '{print $3}'`;
  echo $os;
}

function mkd () {
  if [ ! -d "$1" ]; then
      mkdir -p $1
  fi
}


function puase () {
  read -n 1 -p "Press A-Z key to continue..." INP
  if [[ -n "${INP}" ]] ; then
    echo -ne '\b \n'
  fi
}


function firewall-addport(){
  if [ "`firewall-cmd --zone=public --query-port=$1/tcp`" = "no" ]; then
    printInfo "防火墙添加端口$1,执行结果："`firewall-cmd --permanent --zone=public --add-port=$1/tcp`
    printInfo "防火墙重载，执行结果："`firewall-cmd --reload`
  else
    printInfo "防火墙已存在端口$1";
  fi
}

function firewall-addservice(){
  if [ "`firewall-cmd --zone=public --query-service=$1`" = "no" ]; then
    printInfo "防火墙添加服务$1,执行结果："`firewall-cmd --permanent --zone=public --add-service=$1`
    printInfo "防火墙重载，执行结果："`firewall-cmd --reload`
  else
    printInfo "防火墙已存在服务$1";
  fi
}

function firewall-must-run(){
#  if [[ "`systemctl status firewalld.service|grep Active|awk '{print $2}'`" != "active" ]];
#  then
#    printFail "防火墙未启动！";
#    exit;
#  else
#    printSuccess "防火墙状态正常。";
#  fi
  if [ "`firewall-cmd --state`" != "running" ];
  then
    printFail "防火墙未启动！";
    exit;
  else
    printSuccess "防火墙状态正常。";
  fi
}

function requireJava(){
  if [ ! -f "/opt/jdk/bin/java" ];
  then
bash /home/shell/java-install.sh<<EOF
2
EOF
  fi
}

function requireMysqlCient(){
  if [ ! -f /usr/bin/mysql ];
  then
    if [ $(ostype) == "Ubuntu" ];
    then
      bash /home/shell/ubuntu/mysql-client-install.sh
    else
      echo '暂只支持安装Ubuntu客户端';
      exit;
    fi
  fi
}

function requireGit(){
  if [ ! -f "/usr/bin/git" ]; then
    if [ $(ostype) == "Ubuntu" ]; then
      apt install -y git
    else
      yum install -y git
    fi
  fi
  git config --system --unset credential.helper
  git config --global credential.helper store
  git config --global user.name "江路"
  git config --global user.email jlcon@qq.com
  git config --global core.longpaths true
  git config --global http.sslVerify false
  git config --global pull.rebase false
  git --version
}
function requireMaven(){
  if [ ! -f "/opt/apache-maven/bin/mvn" ]; then
    bash /home/shell/maven-install.sh
  fi
}

function download(){
  if (( $# != 2 ));
  then
    printFail "WGET参数错误，程序退出。";
    exit;
  else
    printInfo "从网络地址：$2\n下载到文件：$1";
  fi
  if [ -f "$1" ]; then
    rm -rf $1
  fi
  wget --content-disposition --no-check-certificate -q --show-progress -O $1 $2
}

function dpkg-install-fromurl(){
  download /tmp/dpkg-install-fromurl-tmp.deb $1
  dpkg -i /tmp/dpkg-install-fromurl-tmp.deb
  rm -rf /tmp/dpkg-install-fromurl-tmp.deb
}

function serversPrint(){
  if [ $(ostype) == "Ubuntu" ]; then
    if [ ! -f "/usr/bin/nmap" ]; then
      apt install -y nmap
    fi
  else
    if [ ! -f "/usr/bin/nmap" ]; then
      yum install -y nmap
    fi
  fi
  router_host=`route -n|grep -i "ug"|awk '{print $2}'|sed -r "s/([0-9]{3}\.[0-9]{2,3}\.[0-9]{1,3}\.).*/\1/g"`;
  lan_hosts=`nmap -sn ${router_host}*|grep "report for"|sed -r "s/Nmap scan report for (.*)/\1/g"`;
  echo -e "\033[96m\033[1m${lan_hosts}\033[0m"
}